package com.hinotoyk.testsystem.controller;

import com.hinotoyk.testsystem.entity.Student;
import com.hinotoyk.testsystem.entity.Teacher;
import com.hinotoyk.testsystem.service.StudentService;
import com.hinotoyk.testsystem.service.TeacherService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;


/**
 * @program: testsystem
 * @description:
 * @author: DYJ
 * @create: 2019-11-16 13:05
 **/
@Controller
public class RegisterController {

    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentService studentService;


    @RequestMapping("/register")
    public String register(Model model, String type){
        if("student".equals(type)){
            model.addAttribute("userType","学生");
        }
        if("teacher".equals(type)){
            model.addAttribute("userType","教师");
        }
        return "Register";
    }

    @RequestMapping("/handleRegister")
    public String handleRegister(String username,String pw,String type){
        if(username==null || username.equals("")|| pw==null || pw.equals("")){
            return "/register?"+"type="+type;
        }

        //用户名作为盐值
        ByteSource salt = ByteSource.Util.bytes(username);
        String md5Password = new SimpleHash("MD5",pw,salt,1024).toHex();
        switch (type){
            case "teacher":
                teacherService.register(new Teacher(username,md5Password));
                break;
            case "student":
                studentService.register(new Student(username,md5Password));
                break;
        }
        return "login";
    }

}
    